package com.novel.utils;

import java.sql.Connection;
import java.sql.SQLException;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public enum Database
{
	DB;
	MysqlDataSource dataSource = null;  
	
	private String host 	= "rm-wz9s6v166b304viwm6o.mysql.rds.aliyuncs.com";
//	private String host 	= "127.0.0.1";
	private int port 	= 3306;
	private String name 	= "xs_common_01";
//	private String name 	= "novel";
	private String username = "pzhx_china"; 
//	private String username = "root"; 
	private String password = "dfgf256_2df";
//	private String password = "root";
	
	private  Database()
	{
		//通过用户配置生成jdbc url
		dataSource = new MysqlDataSource();
    	String url = "jdbc:mysql://" + this.host + ":" + this.port +  "/" + this.name;
    	url = url + "?user=" + this.username + "&password=" + this.password;
    	dataSource.setUrl(url);  
	}
	
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public Connection getConnection() 
	{
		
		Connection dbConn = null;
		try 
		{            
			 if (dataSource == null) 
			 {
		            return null;
		     }
			 dbConn = dataSource.getConnection();
		}        
		catch (Exception e) 
		{           
			e.printStackTrace();
		}        
		return dbConn; 
	}
	
	/**
	 * 关闭链接;
	 * @param dbConn
	 */
	public void close(Connection conn)
	{
		if(conn != null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("关闭资源失败");
				e.printStackTrace();
			}
		}
	}
}